import os
import fitz  # PyMuPDF
import pdfplumber
from pprint import pprint
from collections import OrderedDict
from PIL import Image, ImageDraw, ImageFont
from pdf2paragraph.get_page_tables import get_forms_list
from pdf2paragraph.get_page_images import get_images_list, get_rects_list
from utils.functions import chart_or_form, remove_tables_in_table, remove_duplicates, remove_page_rects,\
    remove_extract_find_tables, remove_images_in_table, remove_rects_in_table


async def extract_table_images(pdf_path, page_num, output_path, file_name):
    '''
    :param pdf_path: pdf路径
    :param page_num: 页码
    :param output_pdf_path: 截取的图片、表格、假图转换成pdf的存储路径
    :param output_images_path: 截取的图片、表格、假图转换成jpg的存储路径
    :param file_name: 文件名
    :return:
    '''

    output_path = "../data/"

    with pdfplumber.open(pdf_path) as pdf:

        page = pdf.pages[page_num]
        # 获取MediaBox
        media_box = page.mediabox  # 直接访问mediabox属性
        # 获取CropBox
        crop_box = page.cropbox  # 直接访问cropbox属性
        print("MediaBox:", media_box)
        print("CropBox:", crop_box)


        page_width = page.width
        page_height = page.height
        print('该页宽度', page_width)
        print('该页高度', page_height)

        extract_tables, find_tables = remove_extract_find_tables(page.extract_tables(), page.find_tables())  # 获取该页基础表格信息 并过滤表格中的假图 只保留表格
        print('原始表格数量', len(find_tables))
        find_tables = remove_tables_in_table(find_tables)  # 移除表格内的表格
        print('清洗表格数量', len(find_tables))

        # text_blocks = page.extract_words()  # 获取该页基础文本信息
        text_blocks = page.extract_text_lines()
        print('len(text_blocks)', len(text_blocks))

        # text_blocks_list = []
        # for text_dict in text_blocks:
        #     text_blocks_dict = {
        #         'text': text_dict['text'],
        #         'top': text_dict['top'],
        #         'bottom': text_dict['bottom'],
        #         'x0': text_dict['x0'],
        #         'x1': text_dict['x1']
        #     }
        #     text_blocks_list.append(text_blocks_dict)
        # pprint(text_blocks_list)
        # x0 > 100 缩进 前面一行+换行符
        # x1 < 500 换行 该行+换行符

        # find_tables = camelot.read_pdf(pdf_path, pages=str(page_num))
        page_images = page.images  # 获取该页基础图片信息
        print('原始图片数量', len(page_images))
        page_images = remove_images_in_table(page_images, find_tables)  # 移除表格内的图片
        print('清洗图片数量', len(page_images))

        page_rects = remove_page_rects(page.rects)  # 获取该页基础矩形信息
        print('原始矩形数量', len(page_rects))
        page_rects = remove_rects_in_table(page_rects, find_tables, page_height, page_width)  # 过滤线条矩形 并移除表格内的矩形后
        print('清洗矩形数量', len(page_rects))

        print('注释数量', len(page.annots))
        print('曲线数量', len(page.curves))
        # curves_list = get_curves_list(page.curves, pdf_path, page_num, output_images_path, output_pdf_path, file_name)
        # print('曲线', page.curves[0])

        print('直线数量', len(page.lines))
        print('超链接数量', len(page.hyperlinks))


    # print('page_images', page_images)

    if len(page_images) == len(find_tables) == len(page_rects) == 0:  # 如果表格和图片和矩形都没有
        print('表格和图片和矩形都没有')
        text_page_list = []
        for text_dict in text_blocks:
            text_blocks_dict = {
                'text': text_dict['text'],
                'top': text_dict['top'],
                'bottom': text_dict['bottom'],
                'x0': text_dict['x0'],
                'x1': text_dict['x1'],
                'x-1': page_width - text_dict['x1'],
                'page_num': page_num
            }
            text_page_list.append(text_blocks_dict)

        # for index, texts in enumerate(text_blocks):  # 遍历该页文本信息 获得该页文本列表 text_page_list = [text]
        #     text_page_list.append(str(texts['text']).strip() + '\n')

    else:   # 如果有表格或者图片
        text_page_list = []
        # for block_index, block in enumerate(text_blocks[:-1]):  # 获取 text_page_list = [{top:'', text:''}]
        for block_index, block in enumerate(text_blocks):  # 遍历该页文本信息 获取 text_page_list = [{top:'', text:''}]
            page_text_dict = {}
            page_text_dict['text'] = block['text']
            page_text_dict['top'] = block['top']
            page_text_dict['bottom'] = block['bottom']
            page_text_dict['x0'] = block['x0']
            page_text_dict['x1'] = block['x1']
            page_text_dict['x-1'] = page_width - block['x1'],
            page_text_dict['page_num'] = page_num
            text_page_list.append(page_text_dict)

        if len(page_rects) == 0:
            print('没有矩形')
        else:
            print('有矩形')
            # 获取 rects_list = [{'顶部': '', '底部': '', '路径': ''}]
            rects_list = get_rects_list(page_rects, pdf_path, page_num, output_path, file_name)

            for rects_index, rects_dict in enumerate(rects_list):
                print(rects_index)
                for index, top_text_dict in enumerate(text_page_list):
                    # if rects_dict['底部'] > text_page_list[-1]['top']:  # 如果矩形底部大于该页最后的文字（页码）顶部 则在text_page_list最后文字前插入矩形路径
                    #     insert_dict = {
                    #         'text': f"<Rects>{rects_dict['路径']}</Rects>" + '\n',
                    #         'top': rects_dict['顶部']}
                    #     text_page_list.insert(-1, insert_dict)
                    #     break

                    if top_text_dict['top'] > rects_dict['顶部'] and top_text_dict['bottom'] < rects_dict['底部']:  # 如果文字顶部大于矩形顶部且文字底部小于矩形底部 则在text_page_list中对应位置替换矩形路径
                        insert_dict = {
                            'text': f"<Rects>{rects_dict['路径']}</Rects>" + '\n',
                            'top': rects_dict['顶部'],
                            'bottom': rects_dict['底部'],
                            'x0': '',
                            'x1': '',
                            'x-1': '',
                            'page_num': page_num

                        }
                        text_page_list[index] = insert_dict
                    elif top_text_dict['top'] > rects_dict['顶部']:  # 如果文字顶部大于矩形顶部 则在text_page_list中对应位置插入矩形路径
                        insert_dict = {
                            'text': f"<Rects>{rects_dict['路径']}</Rects>" + '\n',
                            'top': rects_dict['顶部'],
                            'bottom': rects_dict['底部'],
                            'x0': '',
                            'x1': '',
                            'x-1': '',
                            'page_num': page_num
                        }
                        text_page_list.insert(index, insert_dict)
                        break
                        # break

            text_page_list = remove_duplicates(text_page_list)  # 去重

            # print('========================rects_list=========================')
            # pprint(rects_list)

        if len(page_images) == 0:
            print('没有图片')
        else:
            print('有图片')
            # pprint(text_page_list)
            images_list = get_images_list(page_images, pdf_path, page_num, output_path, file_name)
            for images_index, images_dict in enumerate(images_list):
                print(images_index)
                for index, top_text_dict in enumerate(text_page_list):
                    # if images_dict['底部'] > text_page_list[-1]['top']:
                    #     insert_dict = {
                    #         'text': f"<Images>{images_dict['路径']}</Images>" + '\n',
                    #         'top': images_dict['顶部']}
                    #     text_page_list.insert(-1, insert_dict)
                    #     break

                    if top_text_dict['top'] > images_dict['顶部'] and top_text_dict['bottom'] < images_dict['底部']:  # 如果文字顶部大于图片顶部且文字底部小于图片底部 则在text_page_list中对应位置替换图片路径
                        insert_dict = {
                            'text': f"<Images>{images_dict['路径']}</Images>" + '\n',
                            'top': images_dict['顶部'],
                            'bottom': images_dict['底部'],
                            'x0': '',
                            'x1': '',
                            'x-1': '',
                            'page_num': page_num
                        }
                        text_page_list[index] = insert_dict

                    elif top_text_dict['top'] > images_dict['顶部']:  # 如果文字顶部大于图片顶部 则在text_page_list中对应位置插入图片路径
                        insert_dict = {
                            'text': f"<Images>{images_dict['路径']}</Images>" + '\n',
                            'top': images_dict['顶部'],
                            'bottom': images_dict['底部'],
                            'x0': '',
                            'x1': '',
                            'x-1': '',
                            'page_num': page_num
                        }
                        text_page_list.insert(index, insert_dict)
                        break
                        # break

            text_page_list = remove_duplicates(text_page_list)  # 去重

            # print('========================images_list=========================')
            # pprint(images_list)

        if len(find_tables) == 0:  # 没有表格
            print('没有表格')
        else:
            print('有表格')
            forms_list = get_forms_list(find_tables, page_num, output_path, file_name)
            pprint(forms_list)

            # print('========================forms_list=========================')
            # pprint(forms_list)

            for forms_index, forms_dict in enumerate(forms_list):  # 替换 page_list 中的 表格文本 为 markdown
                print(forms_index)
                for index, top_text_dict in enumerate(text_page_list):
                    if top_text_dict['top'] > forms_dict['顶部'] and top_text_dict['bottom'] < forms_dict['底部']:  # 如果文字顶部大于表格顶部且文字底部小于图片底部 则在text_page_list中对应位置替换图片路径
                        insert_dict = {
                            'text': f"<Tables>{forms_dict['路径']}</Tables>" + '\n',
                            'top': forms_dict['顶部'],
                            'bottom': forms_dict['底部'],
                            'x0': '',
                            'x1': '',
                            'x-1': '',
                            'page_num': page_num
                        }
                        text_page_list[index] = insert_dict

                    elif top_text_dict['top'] > forms_dict['顶部']:  # 如果文字顶部大于图片顶部 则在text_page_list中对应位置插入图片路径
                        insert_dict = {
                            'text': f"<Tables>{forms_dict['路径']}</Tables>" + '\n',
                            'top': forms_dict['顶部'],
                            'bottom': forms_dict['底部'],
                            'x0': '',
                            'x1': '',
                            'x-1': '',
                            'page_num': page_num
                        }
                        text_page_list.insert(index, insert_dict)
                        break
                        
                    # if forms_dict['底部'] >= top_text_dict['top'] >= forms_dict['顶部']:
                    #     text_page_list[index]['text'] = f"<Tables>{forms_dict['路径']}</Tables>" + '\n'
                    #     text_page_list[index]['top'] = forms_dict['顶部']
                        # text_page_list[index]['top'] = (forms_dict['底部'] + forms_dict['顶部']) / 2


            text_page_list = remove_duplicates(text_page_list)  # 去重

            # print('text_page_list')
            # pprint(text_page_list)

            top_add_list = []
            for index, top_text_dict in enumerate(text_page_list):  # 按按'top'升序 获取 page_text
                if top_text_dict['text'].strip()[:8] == '<Tables>':
                    if str(text_page_list[index - 1]['text']).strip()[:2] == '单位':
                        print('T_top', text_page_list[index - 1]['top'])
                        top_add = text_page_list[index - 1]['top']
                        top_add_list.append(top_add)
                    else:
                        print('Tables_top', top_text_dict['top'])
                        top_add_list.append(top_text_dict['top'])

            # print('len(top_add_list)', len(top_add_list))
            #
            # # 打开PDF文件
            # doc = fitz.open(pdf_path)
            # # 读取指定页
            # page = doc[page_num]  # 注意页码是从0开始的
            #
            # page_width = page.rect.width
            # page_height = page.rect.height
            # print(f'该页宽度{page_width}; 高度{page_height}')
            # for index, tables in enumerate(find_tables):
            #     print(f'表格{index}')
            #
            #     # 确保 output_images_path 存在，如果不存在则创建它
            #     os.makedirs(os.path.join(output_path, file_name, 'tables'), exist_ok=True)
            #
            #     images_path = os.path.join(output_path,  file_name, 'tables', f'table_{file_name}_{page_num}_{index}.jpg')
            #
            #     left, top, right, bottom = tables.bbox
            #     print(left, top, right, bottom)
            #     try:
            #         print(max(0, 5), max(0, top_add_list[index] - 5), min(page_width, page_width - 5), min(page_height, bottom + 5))
            #     except:
            #         continue
            #
            #     # 假设你已经知道了表格的边界，这里以一个示例边界为例
            #     # 你需要根据实际情况调整这些值
            #
            #     # table_rect = fitz.Rect(max(0, 3), max(0, top_add_list[index] - 3), min(page_width, page.rect.width - 3), min(page_height, bottom + 3))
            #     table_rect = fitz.Rect(max(0, 5), max(0, top_add_list[index] - 5), min(page_width, page.rect.width - 5), min(page_height, bottom + 5))
            #     # table_rect = fitz.Rect(5, 85, 506, 837)
            #
            #
            #     # table_rect = fitz.Rect(left - 3, bottom - 3, right + 3, top + 3)
            #
            #     # 设置页面的裁剪框
            #     page.set_cropbox(table_rect)
            #
            #     # 创建一个新的PDF文档
            #     new_doc = fitz.open()
            #     new_page = new_doc.new_page(width=table_rect.width, height=table_rect.height)
            #
            #     # 将裁剪的区域复制到新页面
            #     new_page.show_pdf_page(new_page.rect, doc, page.number)
            #
            #     # 确保 output_pdf_path 存在，如果不存在则创建它
            #     # os.makedirs(os.path.join(output_pdf_path, file_name, 'table'), exist_ok=True)
            #     # # 保存新的PDF文件
            #     # new_doc.save(os.path.join(output_pdf_path, file_name, 'table', f'table_{file_name}_{page_num}_{index}.pdf'))
            #
            #     # pix = new_doc[0].get_pixmap(dpi=120)
            #     pix = new_doc[0].get_pixmap(dpi=300)
            #     pix.save(images_path)
            #
            #     #############################################################################
            #     # 保存新的PDF文件
            #     new_doc.close()
            # 关闭原始PDF文档
            # doc.close()

        # text_page_list = []
        # text_page_list = remove_duplicates(text_page_list)  # 去重
        # for index, top_text_dict in enumerate(text_page_list):
        #     text_page_list.append(str(top_text_dict['text']).strip() + '\n')

    return text_page_list


# 使用示例
if __name__ == '__main__':
    # pdf_path = './data/招股说明书.pdf'
    # pdf_path = './data/1662_1.pdf'
    # pdf_path = './data/1363.pdf'
    # file_name = '上海_1836_2'
    # file_name = '上海_1363_2'
    # file_name = '1662_1'
    # file_name = '上海_1409_0'
    # file_name = '问题int'
    # file_name = '8-1 发行人及保荐机构关于审核问询函之回复报告_2023-07-12'
    # file_name = 'RAS_202406_12215042D89595A89F46D9AB957F6688EF250D'
    file_name = '8-1-2 发行人及保荐机构关于第二轮审核问询函的回复_2022-07-18'



    pdf_path = f'../data/{file_name}.pdf'

    # output_pdf_path = "../data/pdf/"
    # output_images_path = "../data/images/"
    output_path = "../data/"

    doc = fitz.open(pdf_path)
    pages_num = len(doc)
    print('总页数', len(doc))

    for page_num in range(len(doc)):
        print('page_num', page_num)
        text_page_list = extract_table_images(pdf_path, 18, output_path, file_name)
        print('========================page_text=========================')
        pprint(text_page_list)
        break

    print('========================结束了=========================')